package com.xiaojf.xlog.appender.rdb;

import com.xiaojf.xlog.appender.AccessLogAppender;
import com.xiaojf.xlog.appender.rdb.entity.AccessLog;
import com.xiaojf.xlog.appender.rdb.service.AccessLogService;
import com.xiaojf.xlog.model.XlogAccessLog;

import java.util.Date;

/**
 * 以数据库形式，保存日志
 *
 * @author Ade.Xiao 2021/3/12 11:38
 */
public class AccessLogRdbAppender implements AccessLogAppender {
    private AccessLogService accessLogService;

    public AccessLogRdbAppender(AccessLogService accessLogService) {
        this.accessLogService = accessLogService;
        System.out.println("使用 rdb 形式记录访问日志");
    }

    @Override
    public void record(XlogAccessLog log) {
        AccessLog entity = new AccessLog();
        entity.setAccessTime(log.getAccessTime());
        entity.setAppendTime(log.getAppendTime());
        entity.setContent(log.getContent());
        entity.setCreatedTime(new Date());
        entity.setUpdatedTime(new Date());
        entity.setOperateType(log.getOperateType().toString());
        entity.setIp(log.getIp());
        entity.setUri(log.getUri());
        entity.setError(log.getError());
        entity.setUri(log.getUri());
        entity.setSuccessful(log.isSuccessful());

        accessLogService.save(entity);
    }
}
